<template>
  <div style="margin: 10px 0">
    <el-input v-model="search" style="width: 20%" placeholder="请输入关键字" clearable/>
    <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    <el-button type="primary" style="float: right;margin-right: 20px" @click="add">新增</el-button>
  </div>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="flowerId" sortable label="ID"/>
    <el-table-column prop="flowerName" label="鲜花名称"/>
    <el-table-column prop="price" label="价格"/>
    <el-table-column prop="groupName" label="种类"/>
    <el-table-column prop="flowerLanguage" label="花语"/>
    <el-table-column prop="description" label="描述"/>
    <el-table-column label="鲜花图片">
<!--      定义了一个默认插槽，它接收一个名为scope的变量。-->
      <template #default="scope">
<!--        当前行的数据scope.row 绑定到当前行数据中的image_url字段-->
<!--        它只在scope.row.image_url存在 没有图片就为空-->
        <img :src="scope.row.imageUrl" style="width: 50px; height: 50px;" v-if="scope.row.imageUrl"/>
      </template>
    </el-table-column>
    <el-table-column prop="createdTime" label="创建时间"/>
    <el-table-column prop="updatedTime" label="修改时间"/>

    <el-table-column fixed="right" label="操作" width="100vh">
      <template #default="scope">
        <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
        <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.flowerId)">
          <template #reference>
            <el-button type="text" style="color: red" >删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>

  <!--  分页-->
  <div style="margin: 10px 0">
    <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20]"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>

    <el-dialog v-model="dialogVisible" title="编辑商品" width="500">
      <el-form :model="form" label-width="80px">
        <el-form-item label="鲜花名称">
          <el-input v-model="form.flowerName" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="鲜花价格">
          <el-input v-model="form.price" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="鲜花种类">
          <el-select v-model="form.groupId" placeholder="请选择" style="width: 80%">
            <el-option
                v-for="item in options"
                :key="item.groupId"
                :label="item.groupName"
                :value="item.groupId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload ref="upload"
                     action="http://localhost:9090/files/upload"
                     :on-success="filesUploadSuccess"
                     :file-list="fileList"
          >
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="花语">
          <el-input type="textarea" v-model="form.flowerLanguage" style="width: 80%"/>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="form.description" style="width: 80%"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确认</el-button>
        </div>
      </template>
    </el-dialog>
</template>

<style scoped>
.pagination {
  width: 400px; /* 或者设置一个具体的宽度，比如300px */
  margin: 30px auto; /* 水平居中 */
  text-align: center;

}
</style>

<script>
import request from "@/utils/request";

export default {
  name: 'Flower',
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      input: '',
      search: '',
      currentPage: 1, // 当前页
      pageSize: 10, // 每页显示条数
      total: 10,
      tableData: [],
      options:[],
      fileList: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    filesUploadSuccess(res){
      this.form.imageUrl = res.data
    },
    load() {
      request.get("/flower/page", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
      //获取类型列表
      request.get("/group/selectAll").then(res => {
        this.options=res.data
        console.log(this.options)
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
      this.fileList = [];
    },
    save() {
      if (this.form.flowerId) {
        //修改
        request.put("/flower", this.form).then(res => {
          if (res.code === '0') {
            this.$message.success("修改成功")
            this.dialogVisible = false
            this.load()
          } else {
            this.$message.error(res.msg)
          }
          console.log(res)
        })
      } else {
        //新增
        request.post("/flower", this.form).then(res => {
          if (res.code === '0') {
            this.$message.success("添加成功")
            this.dialogVisible = false
            this.load()
          } else {
            this.$message.error(res.msg)
          }
          console.log(res)
        })
      }

    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.fileList = [];
    },
    handleDelete(flowerId) {
      request.delete("/flower/" + flowerId).then(res => {
        if (res.code === '0') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleSizeChange() {
      this.load()
    },
    handleCurrentChange() {
      this.load()
    },
    getBirthDate (date) {
      this.form.dateOfBirth = date
    }
  }
}
</script>

